package com.itheima.service.impl;

import com.itheima.dao.MessageMapper;
import com.itheima.dao.UserDao;
import com.itheima.domain.Message;
import com.itheima.domain.User;
import com.itheima.service.MessageService;
import com.itheima.until.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {
    private final MessageMapper messageMapper;
    private final UserDao userInfoMapper;

    @Autowired
    public MessageServiceImpl(MessageMapper messageMapper, UserDao userInfoMapper) {
        this.messageMapper = messageMapper;
        this.userInfoMapper = userInfoMapper;
    }

    /*
     * 消息列表
     * */
    @Override
    public List<Message> conversationList(String userId) {

        // 先查出该用户所有的conversation并按照时间排序
        List<Message> conversationList = messageMapper.findConversationList(userId);

        // 遍历每个conversation来获得信息
        for (Message conversation : conversationList) {
            String conversationId = conversation.getConversationId();
            // 封装未读消息数
            Integer unreadCount = messageMapper.findConversationUnreadCount(conversationId, userId);
            conversation.setUnreadCount(unreadCount);
            // 封装用户信息
            String partnerId = conversationId.replace(userId, "");
            User partner = userInfoMapper.getById(Integer.valueOf(partnerId));
            conversation.setPartner(partner);
            // 封装最新的一条消息
            Message message = messageMapper.findConversationByConversationId(conversationId);
            conversation.setContent(message.getContent());
        }
        // 返回一整个消息列表以json格式传递回前端
        return conversationList;
    }

    /*
     * 聊天列表
     * */
    @Override
    public List<Message> conversation(String userId, String partnerId) {
        String conversationId = getConversationId(userId, partnerId);
//        PageHelper.startPage(pageNum, 7);
        // 根据conversationId查聊天
        List<Message> conversation = messageMapper.findConversation(conversationId);
        // 将当前conversation的status=1
        messageMapper.updateStatus(conversationId, userId);
        for (Message message : conversation) {
            // 封装用户信息
            User partner = userInfoMapper.getById(Integer.valueOf(conversationId.replace(userId, "")));
            message.setPartner(partner);
        }
        return conversation;
//        return new PageInfo<>(conversation);

    }

    @Override
    public void sendMessage(Message message) {
        message.setId(UuidUtil.getShortUuid());

        String conversationId = getConversationId(message.getSendId(), message.getReceiveId());
        message.setConversationId(conversationId);

        message.setTime(new Date());
        message.setStatus(0);
        messageMapper.insert(message);
    }

    /*
     * 用于生成conversationId
     * */
    private String getConversationId(String sendId, String receiveId) {
        // 比较 sendId 和 receiveId，将较小的放在前面
        String smallerId = sendId.compareTo(receiveId) < 0 ? sendId : receiveId;
        String largerId = sendId.compareTo(receiveId) < 0 ? receiveId : sendId;
        // 拼接较小的id和较大的id，以生成 conversationId
        return smallerId + largerId;
    }
}
